package com.ruoyi.student.controller;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.student.domain.StudentFormDto;
import com.ruoyi.student.domain.SubjectFormDto;
import com.ruoyi.teacher.api.domain.Campus;
import com.ruoyi.teaching.api.domain.FocusStudentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.student.domain.SysStudent;
import com.ruoyi.student.service.ISysStudentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生管理Controller
 * 
 * @author Xsw
 * @date 2024-03-23
 */
@RestController
@RequestMapping("/student")
public class SysStudentController extends BaseController
{
    @Autowired
    private ISysStudentService sysStudentService;

    @GetMapping("/lists/getStudentList")
   public List<SysStudent> getStudentList(){
        return sysStudentService.getStudentList();
    }
    /**
     * 查询学生管理列表
     */
    @RequiresPermissions("student:student:list")
    @GetMapping("/list")
    public TableDataInfo list(SysStudent sysStudent)
    {
        startPage();
        List<SysStudent> list = sysStudentService.selectSysStudentList(sysStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生管理列表
     */
    @RequiresPermissions("student:student:export")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStudent sysStudent)
    {
        List<SysStudent> list = sysStudentService.selectSysStudentList(sysStudent);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        util.exportExcel(response, list, "学生管理数据");
    }

    /**
     * 导出学生模版
     * @param response
     */
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<SysStudent> list = new ArrayList<>();
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        util.exportExcel(response, list, "学生中心数据");
    }

    /**
     * 导入学生列表
     */
    @PostMapping("/import")
    public R<Void> importStudent(MultipartFile file) {
        try {
            ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
            List<SysStudent> sysStudent = util.importExcel(file.getInputStream());
            sysStudentService.importStudent(sysStudent);
        } catch (IOException e) {
            e.printStackTrace();
            return R.fail("上传失败");
        }
        return R.ok();
    }

    /**
     * 获取学生管理详细信息
     */
    @RequiresPermissions("student:student:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysStudentService.selectSysStudentById(id));
    }

    /**
     * 新增学生管理
     */
    @RequiresPermissions("student:student:add")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStudent sysStudent)
    {
        return toAjax(sysStudentService.insertSysStudent(sysStudent));
    }

    /**
     * 批量修改学生管理
     */
    @PutMapping("/updateTypes")
    public AjaxResult updateTypes(@RequestBody List<SysStudent> list)
    {
        sysStudentService.updateTypes(list);
        return AjaxResult.success();
    }

    /**
     * 修改学生管理
     */
    @RequiresPermissions("student:student:edit")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStudent sysStudent)
    {
        return toAjax(sysStudentService.updateSysStudent(sysStudent));
    }

    /**
     * 学生留级该状态和班级
     */
    @PutMapping("/repeatedChange")
    public AjaxResult repeatedChange(@RequestBody SysStudent sysStudent)
    {
        return toAjax(sysStudentService.repeatedChange(sysStudent));
    }

    /**
     * 根据学生id修改学生状态
     */
    @PutMapping("/changeStatusById/{studentId}")
    public AjaxResult changeStatusById(@PathVariable Long studentId)
    {
        return toAjax(sysStudentService.changeStatusById(studentId));
    }



    /**
     * 删除学生管理  treeData
     */
    @RequiresPermissions("student:student:remove")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysStudentService.deleteSysStudentByIds(ids));
    }



    /**
     * 根据classId查询学生管理列表
     */
    @GetMapping("/lists/{classId}")
    public TableDataInfo getStudentByClassId(@PathVariable("classId") Long classId)
    {
        List<SysStudent> list = sysStudentService.getStudentByClassId(classId);
        return getDataTable(list);
    }
    /**
     * 查询所有未就业学生
     */
    @GetMapping("/lists/unemployedStudentList")
    public TableDataInfo unemployedStudentList()
    {
        List<SysStudent> list = sysStudentService.unemployedStudentList();
        return getDataTable(list);
    }


    /**
     * 新增重点关注
     */
    @PostMapping("/addStudentFocus")
    public AjaxResult addStudentFocus(@RequestBody FocusStudentDto focusStudentDto)
    {
        sysStudentService.addStudentFocus(focusStudentDto);
        return AjaxResult.success();
    }

    /**
     * 获取所有校区列表
     */
    @GetMapping("/getCampusList")
    public TableDataInfo getCampusList()
    {
        startPage();
        List<Campus> list = sysStudentService.getCampusList();
        return getDataTable(list);
    }


    /**
     * 学生报表按要求查询学生列表
     */
    @PostMapping("/list/getStudentsByFormData")
    public TableDataInfo getStudentsByFormData(@RequestBody StudentFormDto studentFormDto)
    {
        startPage();
        ArrayList<StudentFormDto> list = sysStudentService.getStudentsByFormData(studentFormDto);
        return getDataTable(list);
    }

    /**
     * 学科报表按要求查询学生列表
     */
    @PostMapping("/list/getStudentsByMouth")
    public TableDataInfo getStudentsByMouth(@RequestBody SubjectFormDto subjectFormDto)
    {
        startPage();
        ArrayList<SubjectFormDto> list = sysStudentService.getStudentsByMouth(subjectFormDto);
        return getDataTable(list);
    }
}
